package com.aitaoxue.palx.ui;

import java.util.HashMap;
import java.util.List;

import org.apache.http.Header;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.InjectView;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.api.ApiHttpClient;
import com.aitaoxue.palx.api.remote.AitaoXueApi;
import com.aitaoxue.palx.base.BaseActivity;
import com.aitaoxue.palx.bean.Teacher_All;
import com.aitaoxue.palx.bean.Teacher_All.Content;
import com.aitaoxue.palx.bean.Teacher_All.Course2;
import com.aitaoxue.palx.util.GsonUtil;
import com.aitaoxue.palx.widget.CircleImageView;
import com.aitaoxue.palx.widget.FadingScrollView;
import com.aitaoxue.palx.widget.FadingScrollView.OnScrollListener;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;

public class PersonCenterActivity extends BaseActivity implements Callback,
		OnScrollListener {
	private static final String TAG = PersonCenterActivity.class
			.getSimpleName();

	Teacher_All teacher;

	@InjectView(R.id.header)
	CircleImageView header;
	@InjectView(R.id.name)
	TextView name;
	@InjectView(R.id.sex_and_city)
	TextView sexAndCity;
	@InjectView(R.id.exp_detail)
	TextView expDetail;
	@InjectView(R.id.school_detail)
	TextView schoolDetail;
	@InjectView(R.id.good_at_detail)
	TextView goodAtDetail;
	@InjectView(R.id.address_detail)
	TextView addressDetail;
	@InjectView(R.id.desc)
	TextView desc;
	@InjectView(R.id.course_all)
	LinearLayout courseGroup;
	@InjectView(R.id.scrollview)
	FadingScrollView scrollView;
	@InjectView(R.id.head_view)
	RelativeLayout layout;

	// 保存课程图标的对应关系
	private static HashMap<String, Integer> map;

	private Animation anim;

	private Handler handler;

	private int layout_height;

	private Drawable drawable;

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void initView() {
		handler = new Handler(this);
		anim = AnimationUtils.loadAnimation(this, R.anim.course_item_in);
		map = new HashMap<String, Integer>();
		scrollView.setScrollListener(this);
		drawable = getResources().getDrawable(R.color.header_bg);

		map.put("1", R.drawable.math);
		map.put("2", R.drawable.chinese);
		map.put("3", R.drawable.english);
		map.put("4", R.drawable.biology);
		map.put("5", R.drawable.chemistry);
		map.put("6", R.drawable.physical);
		map.put("7", R.drawable.politics);
		map.put("8", R.drawable.history);
		map.put("9", R.drawable.geogrphy);
		map.put("10", R.drawable.all);
		map.put("11", R.drawable.music);
		map.put("12", R.drawable.art);
		map.put("13", R.drawable.dance);
		map.put("14", R.drawable.language);
		map.put("15", R.drawable.computer);
		map.put("16", R.drawable.hand);
		map.put("17", R.drawable.other);

	}

	@Override
	public void initData() {
		if(getIntent().getBooleanExtra("isMine", false)){
			AitaoXueApi.getProdMyselfInfoByProdId(mHandler);
		}else{
			AitaoXueApi
			.getProdInfo(getIntent().getStringExtra("prod_id"), mHandler);
		}
	}

	@Override
	protected int getLayoutId() {
		return R.layout.person_center;
	}

	@Override
	protected int getActionBarTitle() {
		return R.string.person_center;
	}

	@Override
	protected boolean hasBackButton() {
		return true;
	}

	private AsyncHttpResponseHandler mHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			String res = new String(arg2);

			teacher = GsonUtil.GsonToBean(res, Teacher_All.class);

			handler.obtainMessage().sendToTarget();
		}

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {

		}
	};

	@Override
	public boolean handleMessage(Message msg) {
		Content content = teacher.getContent();
		name.setText(content.getName());
		sexAndCity.setText(content.getSex() + "  " + content.getCity());
		expDetail.setText(content.getExperience());
		schoolDetail.setText(content.getCompany());
		goodAtDetail.setText(content.getGoodat());
		addressDetail.setText(content.getAddress());
		// desc.setText(content.getDescription());

		ImageLoader.getInstance().displayImage(
				ApiHttpClient.HEADER_PREFIX + content.getHead_thumb(), header);

		List<Course2> list = content.getCourse();

		for (final Course2 course : list) {
			handler.postDelayed(new Runnable() {

				@Override
				public void run() {
					View view = LayoutInflater.from(PersonCenterActivity.this)
							.inflate(R.layout.course_item, null);

					ImageView ico = (ImageView) view
							.findViewById(R.id.course_ico);
					TextView name = (TextView) view
							.findViewById(R.id.course_name);
					TextView price = (TextView) view
							.findViewById(R.id.course_price);
					TextView minNum = (TextView) view
							.findViewById(R.id.min_num);

					name.setText(course.getGood_kind_name());
					price.setText("￥" + course.getGood_price() + "/小时");
					minNum.setText("最低起订数:  " + course.getGood_minnum());
					ico.setImageResource(map.get(course.getGood_logo_id()));

					view.startAnimation(anim);
					courseGroup.addView(view);
				}
			}, 1000);
		}

		return false;
	}

	@Override
	public void onScroll(int distance) {
		Log.d(TAG, distance + "");
		float alpha = ((float) 100 / (float) layout_height) * (float) distance
				/ (float) 100;
		Log.d(TAG, "alpha:"+alpha + "");
		if (alpha >= 0.0 && alpha <= 1.0) {
			drawable.setAlpha((int) alpha);
//			getSupportActionBar().setBackgroundDrawable(drawable);
////			getSupportActionBar().setBackgroundDrawable(
////					getResources().getDrawable(R.drawable.ic_launcher));
//			view.setAlpha(1-alpha);
		}
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		layout_height = layout.getHeight();

		super.onWindowFocusChanged(hasFocus);
	}

}
